﻿#region Copyright © 2008, DevLynx (MIT License)
// This software is licensed under The MIT License:
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
// 
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
// 
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
#endregion

namespace DevExpressContrib.Core
{
    using System;
    using System.Net;
    using System.Net.Mail;
    using DevExpressContrib.Core.Interface;

    /// <summary>
    /// SendMail provides a simple method to send an email message via SMTP. The
    /// email can contain any number of attachments.
    /// </summary>
    public static class SendEmail
    {
        /// <summary>
        /// Sends an email message via SMTP.
        /// </summary>
        /// <param name="from">'From' email address.</param>
        /// <param name="to">'To' email address.</param>
        /// <param name="subject">The subject of the email.</param>
        /// <param name="body">The body of the email.</param>
        /// <param name="emailConnection">The email connection information.</param>
        /// <param name="attachments">The optional email attachments.</param>
        /// <exception cref="ArgumentNullException">From or To is null reference.</exception>
        /// <exception cref="ArgumentException">From or To is String.Empty.</exception>
        /// <exception cref="FormatException">From or To is not a well formed email address.</exception>
        /// <exception cref="InvalidOperationException">Host is null or String.Empty; Port is 0; SmptClient is already sending a message via SendAsync.</exception>
        /// <exception cref="ObjectDisposedException">The object has already been disposed.</exception>
        /// <exception cref="SmtpException">SMTP connection failed, authorization failed, or the server timed out.</exception>
        /// <exception cref="SmtpFailedRecipientsException">The message could not be delivered to one of the email recipients.</exception>
        public static void Send(string from, string to, string subject, string body,
            EmailConnectionInformation emailConnection, params EmailAttachment[] attachments)
        {
            MailMessage mailMsg = new MailMessage(from, to, subject, body);
            foreach (EmailAttachment attachment in attachments)
            {
                mailMsg.Attachments.Add(new Attachment(attachment.Value, attachment.Name, attachment.MimeEncoding));
            }

            SmtpClient smtpClient = new SmtpClient(emailConnection.Host, emailConnection.Port);
            if (!String.IsNullOrEmpty(emailConnection.Username))
            {
                NetworkCredential credentials = new NetworkCredential(emailConnection.Username, emailConnection.Password);
                smtpClient.Credentials = credentials;
            }
            smtpClient.Send(mailMsg);
        }
    }
}